<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<head th:include="common/header"></head>
<body>
	<div class="layui-fluid">

		<!-- 导航菜单 -->
		<div th:replace="common/system/system_fragment :: system_menu('系统管理')"></div>

		<!-- 当前位置 -->
		<div th:replace="common/system/system_fragment :: system_location('系统管理', '权限管理')"></div>
		
		<div class="layui-row yy-margin-top-10">
			<div class="layui-col-md4">
				<ul class="ztree" id="tree"></ul>
			</div>
			<div class="layui-col-md8">
				
				<fieldset class="layui-elem-field">
					<legend>基本信息</legend>
					<div class="layui-field-box">
						<form class="layui-form" method="post">
							<div class="layui-colla-item">
								<div class="layui-form layui-form-pane">
									<!-- 权限id -->
									<input type="hidden" id="id" name="id">
									
									<!-- 权限名称 -->
									<div class="layui-form-item">
										<label class="layui-form-label layui-bg-orange">名称</label>
										<div class="layui-input-block">
											<input type="text" name="name" autocomplete="off" class="layui-input" placeholder="名称">
										</div>
									</div>
									
									<!-- 父id -->
									<input type="hidden" id="parentid" name="parentid">
									
									<!-- 顺序 -->
									<div class="layui-form-item">
										<label class="layui-form-label layui-bg-orange">顺序</label>
										<div class="layui-input-block">
											<input type="text" name="seq" autocomplete="off" class="layui-input" placeholder="顺序">
										</div>
									</div>
									
									<!-- 级别 -->
									<div class="layui-form-item">
										<label class="layui-form-label layui-bg-orange">级别</label>
										<div class="layui-input-block">
											<input type="text" name="level" autocomplete="off" class="layui-input" placeholder="级别">
										</div>
									</div>
									
									<!-- 路径 -->
									<div class="layui-form-item">
										<label class="layui-form-label layui-bg-orange">路径</label>
										<div class="layui-input-block">
											<input type="text" name="url" autocomplete="off" class="layui-input" placeholder="路径">
										</div>
									</div>
									
									<!-- 认证码 -->
									<div class="layui-form-item">
										<label class="layui-form-label layui-bg-orange">认证码</label>
										<div class="layui-input-block">
											<input type="text" name="authentication" autocomplete="off" class="layui-input" placeholder="认证码">
										</div>
									</div>
									
									<!-- 备注 -->
									<div class="layui-form-item">
										<label class="layui-form-label layui-bg-orange">备注</label>
										<div class="layui-input-block">
											<input type="text" name="remark" autocomplete="off" class="layui-input" placeholder="备注">
										</div>
									</div>
									
								</div>
							</div>
						</form>
					</div>
				</fieldset>
			
				<div class="layui-form layui-form-pane">
					<a class="layui-btn yy-float-right yy-margin-right-10" onclick="addPermission();" th:each="str,iterStat:${USER_PERMISSION_KEY}" th:if="${str eq 'permission:add'}">
						<i class="layui-icon">&#xe654;</i> 添加
					</a>

					<a class="layui-btn yy-float-right yy-margin-right-10" th:each="str,iterStat:${USER_PERMISSION_KEY}" th:if="${str eq 'permission:update'}" onclick="editPermission();">
						<i class="layui-icon">&#xe642;</i> 编辑
					</a>

					<a class="layui-btn layui-btn-danger yy-float-right" th:each="str,iterStat:${USER_PERMISSION_KEY}" th:if="${str eq 'permission:delete'}" onclick="delPermission();">
						<i class="layui-icon">&#xe640;</i> 删除
					</a>

				</div>
				
			</div>
			
		</div>
		
		
	</div>

	<!-- 引入js -->
	<div th:replace="common/script"></div>
	
	<script th:inline="none">
	/*<![CDATA[*/
	
	//增加权限
	var addPermission = function(){
		var treeObj = $.fn.zTree.getZTreeObj("tree");
		var nodes = treeObj.getSelectedNodes();
		var parentid;
		if(nodes.length > 0){
			parentid = nodes[0].id;
		} else {//如果什么节点都没选择，那说明增加根节点，那么parentid=-1
			parentid = "-1";
		}
		window.location.href="/permission/permissionform/-1/"+parentid;
	};
	
	//编辑权限
	var editPermission = function(){
		var treeObj = $.fn.zTree.getZTreeObj("tree");
		var nodes = treeObj.getSelectedNodes();
		if(nodes.length > 0){
			window.location.href="/permission/permissionform/"+nodes[0].id+"/"+nodes[0].parentid;
		} else {
			$.prompt.alert("请选择要修改的权限!", null);
		}
	};
	
	var setting = {
		async : {
			enable:true,
			dataType:"json",
			autoParam: ["id"],
			url:"/permission/tree"
		},
		data : {
			simpleData:{
				enable:true,
				idKey:"id",
				pIdKey:"parentid",
				rootPId:"-1"
			}
		},
		view : {
			selectedMulti:false,
			showLine:true,
			showTitle:true
		},
		treeId:"tree",
		callback : {
			onClick:function(event, treeId, treeNode){
				var id = treeNode.id;
				$.http.submitForm({
					url:"/permission/get/"+id,
					success:function(r){
						var id = r.id;
						var name = r.name;
						var seq = r.seq;
						var level = r.level;
						var url = r.url;
						var remark = r.remark;
						var authentication = r.authentication;
						var parentid = r.parentid;
						$("input[name=id]").val(id);
						$("input[name=name]").val(name);
						$("input[name=seq]").val(seq);
						$("input[name=level]").val(level);
						$("input[name=url]").val(url);
						$("input[name=remark]").val(remark);
						$("input[name=parentid]").val(parentid);
						$("input[name=authentication]").val(authentication);
						$.logs.info(r);
					}
				});
			}
		}
	};
		
	$(function(){
		//初始化权限树
		$.fn.zTree.init($("#tree"), setting);
		
	});
		
	//删除权限
	function delPermission(){
		var treeObj = $.fn.zTree.getZTreeObj("tree");
		var nodes = treeObj.getSelectedNodes();
		var id;
		if(nodes.length > 0){
			var isParent = nodes[0].isParent;
			if(isParent){
				$.prompt.alert("请选择叶子节点，只可以删除叶子节点!");
				return;
			}
			id = nodes[0].id;
			$.prompt.delPromptBox("确定要删除吗？", "/permission/delpermission/"+id, "/permission/permissionList");
		} else {//如果什么节点都没选择，那说明增加根节点，那么parentid=-1
			$.prompt.alert("请选择要删除的权限!");
		}
		
	}
	
	/*]]>*/
	</script>
	
</body>

</html>